package com.qf.blogsystem.core.controller;

import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 管理页面跳转的servlet
 */
@WebServlet("/PageServlet/*")
public class PageServlet extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过请求地址获取跳转的页面
        String contextPath = request.getContextPath();
        String path = request.getRequestURI().replace(contextPath+"/PageServlet/", "");
        //获得thymeleaf跳转方式中的viewName
        String page=path.replace(".html", "");
        //创建WebContext对象
        WebContext webContext = new WebContext(request, response, this.getServletContext());
        // 用thymeleaf方式跳转到指定的页面
        templateEngine.process(page, webContext, response.getWriter());

    }
}
